﻿using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using HelperTools;
using Interfaces.Filters;
using Interfaces.Models;
using Interfaces.RepositoryModels.InternalModels.EconomicInformationModels;

namespace SQLRepositories
{
    internal  static class RepositoryMappingHelper
    {
        public static void ResetMapping()
        {
            Mapper.Reset();
        }

        public static void CreateQuoteMapping()
        {
            Mapper.CreateMap<QuoteModel, IQuote>()
                  .ForMember(x => x.Company, opt => opt.Ignore());
        }

        public static void CreateCompanyMapping(bool isFullLoading, IFilter filter)
        {
            Mapper.CreateMap<CompanyModel, ICompany>()
                      .ForMember(x => x.Industry, opt => opt.Ignore())
                      .ForMember(x => x.Quoteis, opt => opt.Ignore())
                      .AfterMap((s, d) =>
                      {
                          if (isFullLoading)
                          {
                              d.Quoteis = Mapper.Map<IEnumerable<QuoteModel>, IEnumerable<IQuote>>(s.Quoteis) as ICollection<IQuote>;
                              d.Quoteis = (filter.Returns(x => x.Filtrate(d.Quoteis), d.Quoteis) as IEnumerable<IQuote>).With(x => x.ToList());

                              if (d.With(x => x.Quoteis) != null)
                              {
                                  foreach (var quote in d.Quoteis)
                                      quote.Company = d;
                              }

                          }
                      });
        }

        public static void CreateIndustryMapping(bool isFullLoading, IFilter filter)
        {
            Mapper.CreateMap<IndustryModel, IIndustry>()
                    .ForMember(x => x.Companies, opt => opt.Ignore())
                    .ForMember(x => x.Sector, opt => opt.Ignore())
                    .AfterMap((s, d) =>
                    {
                        if (isFullLoading)
                        {
                            d.Companies = Mapper.Map<IEnumerable<CompanyModel>, IEnumerable<ICompany>>(s.Companies) as ICollection<ICompany>;
                            d.Companies = (filter.Returns(x => x.Filtrate(d.Companies), d.Companies) as IEnumerable<ICompany>).With(x => x.ToList());

                            if (d.With(x => x.Companies) != null)
                            {
                                foreach (var company in d.Companies)
                                    company.Industry = d;
                            }

                        }

                    });
        }

        public static void CreateSectorsMapping(bool isFullLoading, IFilter filter)
        {
            Mapper.CreateMap<SectorModel, IIndustrySector>()
                      .ForMember(x => x.Industries, opt => opt.Ignore())
                      .AfterMap((s, d) =>
                      {
                          if (isFullLoading)
                          {
                              d.Industries = Mapper.Map<IEnumerable<IndustryModel>, IEnumerable<IIndustry>>(s.Industries) as ICollection<IIndustry>;
                              d.Industries = (filter.Returns(x => x.Filtrate(d.Industries), d.Industries) as IEnumerable<IIndustry>).With(x => x.ToList());

                              if (d.With(x => x.Industries) != null)
                              {
                                  foreach (var industry in d.Industries)
                                      industry.Sector = d;
                              }

                          }
                      });
        }

        public static void CreateXChangeRateMapping()
        {
            Mapper.CreateMap<XchangeRateModel, IXchangeRate>()
                  .ForMember(x => x.Pair, opt=>opt.Ignore());
        }

        public static void CreatePairMapping(bool isFullLoading, IFilter filter)
        {
            Mapper.CreateMap<PairModel, IPair>()
                  .ForMember(x => x.Rateses, opt => opt.Ignore())
                  .AfterMap((s, d) =>
                      {
                          if (isFullLoading)
                          {
                              d.Rateses = Mapper.Map<IEnumerable<XchangeRateModel>, IEnumerable<IXchangeRate>>(s.Rateses) as ICollection<IXchangeRate>;
                              d.Rateses = (filter.Returns(x => x.Filtrate(d.Rateses), d.Rateses) as IEnumerable<IXchangeRate>).With(x => x.ToList());

                              if (d.With(x => x.Rateses) != null)
                              {
                                  foreach (var rate in d.Rateses)
                                      rate.Pair = d;
                              }
                          }
                      });
        }
    }
}
